package com.dawnClass.service.impl;

import com.dawnClass.VO.OrderOrderDetailVO;
import com.dawnClass.mapper.ConsumerOrderMapper;
import com.dawnClass.mapper.CourseMapper;
import com.dawnClass.pojo.*;
import com.dawnClass.service.OrderService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ConsumerOrderMapper consumerOrderMapper;
@Autowired
private CourseMapper courseMapper;



    @Override
    public Integer countOrder(Integer consumerId) {
        ConsumerOrderExample orderExample = new ConsumerOrderExample();
        orderExample.createCriteria().andConsumerIdEqualTo(consumerId);
        List<ConsumerOrder> orderList = consumerOrderMapper.selectByExample(orderExample);
        return orderList.size();
    }
    @Override
    public ServiceResponse getOrderByConsumerId(Integer consumerId,int status) {
        ConsumerOrderExample orderExample = new ConsumerOrderExample();
        orderExample.createCriteria().andConsumerIdEqualTo(consumerId);
        List<ConsumerOrder> orders = consumerOrderMapper.selectByExample(orderExample);
        List<OrderOrderDetailVO> orderOrderDetailVOS = new ArrayList<>();


//（0删除 ，1未支付 ，2已完成 ，3拼团失败 ，4拼团中 ，5拼团成功）
//        myorder status筛选
//（0全部，1未支付，2已完成，3拼团）
//        if ( status = 0 ){
//            betweenAnd(1,2)
//            查询全部订单
//        }else if(1){
//            pateto(1)
//        }
//        else if(2){
//            pateto(2)
//        }
//        else if(3){
//            betweenAnd(3,5)
//        }

        if (status == 0) {
            for (ConsumerOrder order : orders) {
                if (order.getOrdPayState() > 0 && order.getOrdPayState() < 4) {
                    Course course = courseMapper.selectByPrimaryKey(order.getCourseId());
                    orderOrderDetailVOS.add(new OrderOrderDetailVO(order, course));
                }
            }
            if (orders.isEmpty()) {
                return ServiceResponse.getFail(null);
            } else {
                return ServiceResponse.getSuccess(orderOrderDetailVOS);
            }
        }
        if (status == 1) {
            for (ConsumerOrder order : orders) {
                if (order.getOrdPayState() == 2) {
                    Course course = courseMapper.selectByPrimaryKey(order.getCourseId());
                    orderOrderDetailVOS.add(new OrderOrderDetailVO(order, course));
                }
            }
            if (orders.isEmpty()) {
                return ServiceResponse.getFail(null);
            } else {
                return ServiceResponse.getSuccess(orderOrderDetailVOS);
            }
        }
        if (status == 2) {
            for (ConsumerOrder order : orders) {
                if (order.getOrdPayState() == 3) {
                    Course course = courseMapper.selectByPrimaryKey(order.getCourseId());
                    orderOrderDetailVOS.add(new OrderOrderDetailVO(order, course));
                }
            }
            if (orders.isEmpty()) {
                return ServiceResponse.getFail(null);
            } else {
                return ServiceResponse.getSuccess(orderOrderDetailVOS);
            }
        }
        if (status == 3){
            for (ConsumerOrder order : orders) {
                if (order.getOrdPayState() == 1) {
                    Course course = courseMapper.selectByPrimaryKey(order.getCourseId());
                    orderOrderDetailVOS.add(new OrderOrderDetailVO(order, course));
                }
            }
            if (orders.isEmpty()) {
                return ServiceResponse.getFail(null);
            } else {
                return ServiceResponse.getSuccess(orderOrderDetailVOS);
            }
        }
        if (status == 4){
            for (ConsumerOrder order : orders) {
                if (order.getOrdPayState()> 3) {
                    Course course = courseMapper.selectByPrimaryKey(order.getCourseId());
                    orderOrderDetailVOS.add(new OrderOrderDetailVO(order, course));
                }
            }
            if (orders.isEmpty()) {
                return ServiceResponse.getFail(null);
            } else {
                return ServiceResponse.getSuccess(orderOrderDetailVOS);
            }
        }
        return null;
    }


    @Override
    public ServiceResponse deletebyId(Integer orderId) {
        ConsumerOrder order = consumerOrderMapper.selectByPrimaryKey(orderId);
        order.setOrdPayState(0);
        int rows =  consumerOrderMapper.updateByPrimaryKey(order);
        if(rows >0)
            return ServiceResponse.updataSuccess(rows);
        else
            return ServiceResponse.updataFail(null);
    }

    @Override
    public OrderOrderDetailVO getById(Integer orderId) {
        ConsumerOrder consumerOrder = consumerOrderMapper.selectByPrimaryKey(orderId);
        Course course = courseMapper.selectByPrimaryKey(consumerOrder.getCourseId());
        OrderOrderDetailVO orderOrderDetailVO = new OrderOrderDetailVO(consumerOrder,course);
        return orderOrderDetailVO;
    }
    @Override
    public ServiceResponse getByOrdId(Integer orderId){
        ConsumerOrder consumerOrder = consumerOrderMapper.selectByPrimaryKey(orderId);
        Course course = courseMapper.selectByPrimaryKey(consumerOrder.getCourseId());
        OrderOrderDetailVO orderOrderDetailVO = new OrderOrderDetailVO(consumerOrder,course);
        if (orderOrderDetailVO !=null) {
            return ServiceResponse.getSuccess(orderOrderDetailVO);
        } else {
            return ServiceResponse.getFail(null);
        }
    }


}
